package com.boe.csb.core.test.service;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;

import com.boe.csb.core.common.status.CertificationStatus;
import com.boe.csb.core.common.utils.MD5Utils;
import com.boe.csb.core.dao.BoeUserMapper;
import com.boe.csb.core.entity.SimplePageInfo;
import com.boe.csb.core.entity.po.ApiCategory;
import com.boe.csb.core.entity.po.BoeAppBean;
import com.boe.csb.core.entity.po.BoeUser;
import com.boe.csb.core.service.api.ApiCategoryService;
import com.boe.csb.core.service.api.BoeApiInfoService;
import com.boe.csb.core.service.csb.CsbManagerApiService;
import com.boe.csb.core.service.csb.CsbManagerOrderService;
import com.boe.csb.core.service.user.BoeAppService;
import com.boe.csb.core.service.user.BoeCloudLogService;
import com.boe.csb.core.service.user.BoeUserService;

@ContextConfiguration(locations = "/boe-test.xml")
public class BoeStartTask extends AbstractTransactionalJUnit4SpringContextTests{
	
	@Autowired
	private CsbManagerApiService csbManagerApiService;
	
	@Autowired
	private BoeUserService boeUserService;
	
	@Autowired
	private BoeUserMapper boeUserMapper;
	
	@Autowired
    private BoeApiInfoService boeApiInfoService;
	@Autowired
	private BoeAppService boeAppService;
	  @Autowired
	    private ApiCategoryService apiCategoryService;
	
	  @Autowired
	  private CsbManagerOrderService csbManagerOrderService;
	  
	  @Autowired
	  private BoeCloudLogService boeCloudLogService;
	//@Test
	//查看服务列表
	public void test(){
		String userId = "1804367502749050";
		Integer pageNum = 1;
		String serviceName = null;
		String alias = null;
		csbManagerApiService.ownPublishedService(userId, pageNum, serviceName, alias);
	}
	
	//@Test
	//查找单个服务
	public void test1(){
		String userId = "1804367502749054";
		Long serviceId = (long)1107;//452//455//458//459
//		csbManagerApiService.findSingleService(userId, serviceId);
		csbManagerOrderService.queryOrderStatus(userId, serviceId, "exchange_rate");
	}
	
	//@Test
	//更新服务
	public void test2() throws IOException{
		String userId = "1804367502749041";
		String data = "{\"policyHandler\":\"accept\",\"sequenceId\":464,\"categoryId\":72,\"isAudit\":true,\"id\": 464,\"serviceName\": \"get_service_info_api_tianjin_test10\",\"serviceVersion\": \"1.0.0\",\"alias\": \"中星河测试\",\"scope\": \"0\",\"allVisiable\": true,\"qps\": 100, \"description\": \"星河测试服务\",\"createTime\": \"2016-12-24 20:30:46\",\"modifiedTime\": \"2017-03-02 20:30:46\",\"ottFlag\": false, \"interfaceName\": \"\",\"mockResult\": \"\",  \"isSSL\": false, \"errList\": [],\"provideType\": \"Restful\",\"consumeTypes\":[\"Restful\"],\"inputParamList\": [{\"targetName\": \"serviceId\",\"defaultValue\": \"0L\",\"description\": \"\",\"catType\": 0,\"optional\": true,\"sample\": \"\",\"originalName\": \"serviceId\",\"paramType\": \"java.lang.Long\",\"realParamType\": \"java.lang.Long\",\"depth\": 0,\"mapStyle\": 1,\"passMethod\": \"GET\",\"extType\": 0}],\"outputParamList\": [{\"targetName\": \"serviceId\",\"defaultValue\": \"0L\",\"description\": \"\",\"catType\": 0,\"optional\": true,\"sample\": \"\",\"originalName\": \"serviceId\",\"paramType\": \"java.lang.Long\",\"realParamType\": \"java.lang.Long\",\"depth\": 0,\"mapStyle\": 1,\"passMethod\": \"GET\",\"extType\": 0}],\"visiableGroups\": [], \"ipBlackStr\": \"null\",     \"ipWhiteStr\": \"null\", \"approveUserId\": \"1804367502749047\", \"approveComments\": \"OK\",\"accessEndpointJSON\": \"{\\\"requestFormat\\\":\\\"HTTP\\\",\\\"responseFormat\\\":\\\"passThrough\\\",\\\"method\\\":\\\"GET\\\",\\\"endpoint\\\":\\\"http://10.80.25.195/api/info/detail\\\"}\"}";
		System.out.println(data);
		csbManagerApiService.publishService(userId, data);
	}
	
//	@Test
//	重置密码
	public void test3(){
		try{
			Long userId = (long)140;
			String adminPassword = "123456";
			boeUserService.resetPassword(userId, adminPassword);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
//	@Transactional
//	@Test
//	@Rollback(false)
	//新增账号
	public void test4(){
		String adminId = "admin";
		BoeUser boeUser = new BoeUser();
        boeUser.setEmail("xinghe_1");
        boeUser.setQq("xinghe@dts.com");
        boeUser.setActiveStatus(Byte.valueOf("1"));
        boeUser.setPassword(MD5Utils.getMD5String("123456"));
        boeUser.setAuditStatus(CertificationStatus.ALLOW.getStatus());
        boeUser.setIsAdmin(true);
        boeUser.setCantModify(Boolean.FALSE);
		boeUserService.createAdmin(adminId, boeUser);
	}
	
//	@Transactional
//	@Test
//	@Rollback(false)
	//新增账号
	public void test5(){
		boeUserMapper.updateAdminUserPower(Long.valueOf("140"),"1","1");
	}
	
//	@Transactional
//	@Test
//	@Rollback(false)
	//发布和订阅发送邮件
	public void test6(){
		csbManagerApiService.sendAdminUserMail("subscribe","455","153");
	}
	
	//新增app服务
	//@Test
	public void test7(){
		boeAppService.loadAppInfo(1,5,null);
	}
	
	//@Test
	public void test8(){
		ApiCategory apiCategory = apiCategoryService.queryCategoryById(Long.valueOf(69));
		System.out.println(apiCategory);
        Map<String,String> map = apiCategoryService.getCatagoryInfo(apiCategory);
        System.out.println(map.toString());
	}
	//@Test
	public void main11(){
		String ss = "asd123456";
		System.out.println(ss.endsWith("456"));
	}
	
	public static void main2(String args[]){
		System.out.println("xinghe@dtstack.com".endsWith("dtstack.com"));
	}
	
	//@Test
	public void addAppInfo(){
		Date time = new Date();
        String sql_id = "(";
        BoeAppBean boeapp = new BoeAppBean();
        boeapp.setName("12121212");
        String icon = "asdasd?id=327&type=asd";
        if(!StringUtils.isBlank(icon)){
        	sql_id += returnNum(icon);
        }
        boeapp.setIcon(icon);
        boeapp.setSize("12");
        boeapp.setVersion("1.2.3");
        boeapp.setProductExplain("zxzzzzzzzzzzz");
        String qrAndroid = "asdasd?id=328&type=asd";
        if(!StringUtils.isBlank(qrAndroid)){
        	sql_id += ","+returnNum(qrAndroid);
        }
        boeapp.setQrAndroid(qrAndroid);
        String qrIos = "asdasd?id=327&type=asd";
        if(!StringUtils.isBlank(qrIos)){
        	sql_id += ","+returnNum(qrIos);
        }
        boeapp.setQrIos(qrIos);
        String oneDisplay = "asdasd?id=327&type=asd";
        if(!StringUtils.isBlank(oneDisplay)){
        	sql_id += ","+returnNum(oneDisplay);
        }
        boeapp.setOneDisplay(oneDisplay);
        String twoDisplay = "asdasd?id=327&type=asd";
        if(!StringUtils.isBlank(twoDisplay)){
        	sql_id += ","+returnNum(twoDisplay);
        }
        boeapp.setTwoDisplay(twoDisplay);
        String threeDisplay = "asdasd?id=327&type=asd";
        if(!StringUtils.isBlank(threeDisplay)){
        	sql_id += ","+returnNum(threeDisplay);
        }
        boeapp.setThreeDisplay(threeDisplay);
        String fourDisplay = "asdasd?id=327&type=asd";
        if(!StringUtils.isBlank(fourDisplay)){
        	sql_id += ","+returnNum(fourDisplay);
        }
        boeapp.setFourDisplay(fourDisplay);
        boeapp.setIsDisplay("1");
        boeapp.setIsInvalid(0);
        boeapp.setCreateName("asdasd");
        boeapp.setCreateTime(time);
        
        
        sql_id += ")";    	
		boeAppService.addAppInfo(boeapp, sql_id);
		
	}
	
    private static String returnNum(String str){
    	String regEx="\\d+";   
    	Pattern p = Pattern.compile(regEx);
    	Matcher m = p.matcher(str);
    	while (m.find()) {  
            return m.group(0);  
        }  
    	return "0";
    }
    
	@Test
    public void test22(){
//		SimplePageInfo sim = boeCloudLogService.queryCloudErrorLog("1493893200000", "1493979600000",10,1);
		
		SimplePageInfo sim = boeCloudLogService.queryStatisticsLog("1494397983187", "1494484983187", "", "", "", 5, 1,0);
		System.out.println(sim);
		System.out.println(sim.getTotal());
		System.out.println(sim.getList().get(0).toString());
		System.out.println(sim.toString());
//		System.out.println(ResultModel.success(sim).getData().getList());
    }
    //@Test
	public void test23(){
    	List<String> list = boeUserMapper.loadApiName("%@%");
    	System.out.println(list.size());
    	System.out.println(list.get(0));
	}
    
    
}
